{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-30T03:10:26.802134Z",
     "start_time": "2025-05-30T03:10:22.758002Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "def euclidean_distance(point1, point2):\n",
    "    return np.sqrt(np.sum((point1 - point2) ** 2))\n",
    "\n",
    "def find_closest_clusters(clusters):\n",
    "    min_distance = float('inf')\n",
    "    closest_clusters = None\n",
    "    for i in range(len(clusters)):\n",
    "        for j in range(i + 1, len(clusters)):\n",
    "            distance = euclidean_distance(clusters[i][0], clusters[j][0])\n",
    "            if distance < min_distance:\n",
    "                min_distance = distance\n",
    "                closest_clusters = (i, j)\n",
    "    return closest_clusters\n",
    "\n",
    "def agglomerative_clustering(data, num_clusters):\n",
    "    clusters = [[point] for point in data]\n",
    "    while len(clusters) > num_clusters:\n",
    "        i, j = find_closest_clusters(clusters)\n",
    "        clusters[i].extend(clusters[j])\n",
    "        del clusters[j]\n",
    "    return clusters\n",
    "\n",
    "# 生成一些示例数据\n",
    "def generate_data():\n",
    "    np.random.seed(42)\n",
    "    center1 = np.array([1, 1])\n",
    "    center2 = np.array([5, 5])\n",
    "    data1 = np.random.normal(loc=center1, scale=0.5, size=(100, 2))\n",
    "    data2 = np.random.normal(loc=center2, scale=0.5, size=(100, 2))\n",
    "    data = np.vstack((data1, data2))\n",
    "    return data\n",
    "\n",
    "# 主程序\n",
    "data = generate_data()\n",
    "num_clusters = 2\n",
    "clusters = agglomerative_clustering(data, num_clusters)\n",
    "\n",
    "# 打印结果\n",
    "for i, cluster in enumerate(clusters):\n",
    "    print(f\"Cluster {i + 1}: {cluster}\")\n"
   ],
   "id": "51d44bf378956b58",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cluster 1: [array([1.24835708, 0.93086785]), array([1.23661881, 0.96358554]), array([1.1287752 , 0.96277704]), array([1.16204198, 0.80745886]), array([1.04352353, 0.85049632]), array([1.00255673, 0.88270643]), array([1.04982568, 0.74826217]), array([1.18081801, 0.67744012]), array([1.14653624, 0.64282429]), array([1.36923329, 1.08568414]), array([1.45770106, 1.16437555]), array([1.48168806, 1.20639046]), array([1.14806014, 1.13052764]), array([1.12524643, 1.1732241 ]), array([1.08659046, 1.19265869]), array([1.26097078, 1.14849234]), array([0.88292331, 0.88293152]), array([0.94217586, 0.84944815]), array([0.76829115, 0.76713512]), array([0.69968066, 0.85415313]), array([0.64897345, 0.83616893]), array([0.76041288, 0.90717051]), array([0.82864274, 0.59886137]), array([0.87730594, 0.62313192]), array([0.58039124, 0.84539381]), array([0.60837335, 0.83896924]), array([0.5957532 , 0.74912148]), array([0.50473184, 0.71685114]), array([0.55524279, 0.59209486]), array([1.73282438, 0.88711185]), array([1.73894702, 0.74086489]), array([1.94309295, 1.08728891]), array([1.93288726, 1.23691646]), array([0.76526281, 1.27128002]), array([0.7351199 , 1.25663372]), array([0.661539  , 1.30583814]), array([0.88826861, 1.35700025]), array([0.89016406, 1.17855629]), array([0.90381952, 1.15077367]), array([0.91935714, 1.20202543]), array([0.96144915, 1.17057599]), array([0.72780864, 1.05546129]), array([0.65998764, 1.11612685]), array([0.76968061, 1.52856111]), array([0.77674252, 1.4281994 ]), array([0.96399494, 1.50176645]), array([1.04853877, 1.4843225 ]), array([0.49358444, 1.15712367]), array([0.42450321, 1.18784901]), array([0.46884814, 1.23679622]), array([0.46455375, 1.24123621]), array([0.55807128, 1.07686255]), array([0.33590698, 1.09843062]), array([0.22466828, 1.03428149]), array([0.19625838, 1.09231693]), array([0.40434825, 1.3282768 ]), array([0.51265916, 1.3935423 ]), array([1.32384427, 1.76151493]), array([1.40626291, 1.67812001]), array([1.1806978 , 1.76901828]), array([1.11372997, 1.65357138]), array([1.16563172, 1.48777256]), array([1.1299414 , 1.39091144]), array([1.1383454 , 1.41359162]), array([1.78960641, 1.38371736]), array([1.51549976, 1.46564006]), array([1.57141141, 1.37596652]), array([0.69914669, 1.92613909]), array([0.54028788, 1.7749672 ]), array([0.98208698, 1.78232183]), array([1.00650095, 1.72676704]), array([1.0301151 , 2.23162106]), array([0.86767158, 2.36008458]), array([1.29342855, 2.09522781]), array([1.41103008, 1.94839649]), array([1.12098114, 0.04335988]), array([1.1044318 , 0.02016494]), array([1.04588039, 0.00621554]), array([1.17180914, 0.11847992]), array([1.0337641 , 0.28762591]), array([1.10704687, 0.37713061]), array([0.99325139, 0.47114454]), array([0.98264412, 0.41566098]), array([1.02910436, 0.42851485]), array([0.80394592, 0.26824253]), array([1.41127246, 0.38957818]), array([1.40675861, 0.38456784]), array([1.39551597, 0.54530627]), array([1.31283367, 0.57142122]), array([1.57929779, 0.58965884]), array([1.70139716, 0.29907447]), array([0.13754108, 0.71885624]), array([0.260739 , 0.6400779]), array([0.29231463, 0.78967734]), array([0.04061439, 0.98674306]), array([0.54598796, 0.29384815]), array([0.57660314, 0.24257639]), array([0.44683251, 0.40189669]), array([0.38152464, 0.33977169]), array([-0.30987255,  1.41095125])]\n",
      "Cluster 2: [array([5.17889368, 5.28039226]), array([5.25751763, 5.25689298]), array([5.31205991, 5.31417275]), array([5.05675867, 5.33106534]), array([5.04849798, 5.29757851]), array([4.9895492 , 5.05866369]), array([4.84236538, 5.37948461]), array([4.92410745, 5.2941586 ]), array([4.94561993, 5.20085586]), array([5.01225509, 5.24899915]), array([4.76403407, 5.5444753 ]), array([4.8911594 , 5.54938843]), array([4.94273008, 5.61890816]), array([4.64234815, 5.33979887]), array([4.66910677, 5.42621667]), array([4.70531762, 5.42480105]), array([4.50924567, 5.23105174]), array([4.55079266, 5.24595959]), array([5.54152562, 5.52690103]), array([5.7255718 , 5.47963541]), array([5.47700088, 5.32569563]), array([5.40643106, 5.31481442]), array([5.3736468 , 5.30518513]), array([5.41270817, 5.40675482]), array([5.57905544, 5.39583135]), array([5.44979994, 5.15364976]), array([5.43616032, 5.091671  ]), array([5.28544526, 5.56778282]), array([5.25249364, 5.4328776 ]), array([5.22190971, 5.38731703]), array([5.15545378, 5.73767811]), array([5.25967326, 5.76636946]), array([5.17087799, 5.93808542]), array([6.07197204, 5.31695951]), array([6.0610781 , 5.51623263]), array([5.88272712, 5.20249086]), array([4.59088966, 6.04619364]), array([4.3398834 , 5.91572938]), array([4.31116532, 4.53108748]), array([4.49699131, 4.39290569]), array([4.27595783, 4.29626811]), array([4.61358739, 4.8815907 ]), array([4.64077789, 4.89327642]), array([4.5872514 , 4.83930708]), array([4.60373963, 4.94263178]), array([4.53653476, 4.97023732]), array([4.58550249, 4.71990948]), array([4.58013908, 4.70030368]), array([4.75731823, 5.04093707]), array([4.77997776, 5.06537029]), array([4.76448085, 5.11602497]), array([4.63481668, 5.10822929]), array([4.62043367, 5.07519689]), array([4.5888898 , 5.12184361]), array([4.3998518 , 4.83274938]), array([4.24031502, 4.75788296]), array([4.76252734, 4.67333538]), array([3.98742871, 5.09322716]), array([3.93805214, 4.73712249]), array([4.36955802, 5.45893097]), array([4.14343274, 5.67693619]), array([3.37936633, 4.48780618]), array([6.15732928, 4.0663674 ]), array([6.06651669, 4.0239561 ]), array([5.3431301 , 4.19364206]), array([5.03214001, 4.46112761]), array([4.99387661, 4.55137281]), array([4.9904919 , 4.49873532]), array([4.87371592, 4.37610841]), array([5.02278592, 4.67419983]), array([5.03790228, 4.66141914]), array([5.04883805, 4.61349511]), array([5.09952985, 4.69989156]), array([5.14049593, 4.68865024]), array([5.12248329, 4.74652841]), array([5.03490104, 4.8073432 ]), array([4.99074343, 4.85567068]), array([4.89593887, 4.75349953]), array([5.17850774, 4.6535452 ]), array([5.16135928, 4.58638453]), array([5.20646573, 4.71813772]), array([5.63345557, 4.64616527]), array([5.63883245, 4.70421431]), array([5.47521192, 4.71154817]), array([5.58972006, 4.76541217]), array([5.58158188, 5.00511653]), array([5.6527394 , 5.01050192]), array([5.48755987, 4.92647131]), array([5.42882981, 4.92003074]), array([5.27354869, 4.89890367]), array([5.34097649, 4.84486662]), array([5.345072  , 4.79938976]), array([5.16208318, 4.93492847]), array([5.11204624, 5.0062962 ]), array([5.81620565, 4.28492931]), array([5.72063664, 4.28206892]), array([5.79300841, 4.38109225]), array([6.07659123, 4.61632622]), array([6.09490147, 4.59585086]), array([5.25752384, 6.92636575])]\n"
     ]
    }
   ],
   "execution_count": 7
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
